<?php

namespace App\Http\Api\Controllers\Order;

use App\Exceptions\InvalidRequestException;

use App\Http\Api\Services\FxgService;
use App\Http\Controllers\Controller;
use App\Http\Models\Product;
use App\Http\Models\ProductSku;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use UpdateHelper\NotUpdateInterfaceInstanceException;


class FxgGoodsApiController extends Controller
{
    protected $fxgService;

    public function __construct(FxgService $fxgService)
    {
        $this->fxgService = $fxgService;
    }

//    public function putGoods(Request $request){
//
//        $data = $request->input('data') ?? '';
//        $result =   [];
//        if(!empty($data)){
//            /*
//             * 1、规格上传，得spec_id
//             * 2、产品上传
//             * 3、上传sku
//             */
//            $shops  =   file_get_contents('http://order.youdnr.com/api/temai/order/fxg_shop_list');
//            $shops  =   json_decode($shops,true);
//            $shops  =   $shops['result'];
//            $shops  =   collect($shops)->whereIn('shop_id',$data['shop_ids'])->all();
//
//            foreach ($shops as $k=>$v){
//                $result_spec    =   $this->upGoodsSpec($data);
//                $spec_id        =   $result_spec['data']['id'] ?? ''; //规格id
//                $skus           =   $this->makeSkus($result_spec,$data);
//                $result_goods  =   $this->upGoods($data,$spec_id,$skus);//上传商品
//                $result_skus     =   $this->upGoodsSkus($spec_id,$skus,$data);//上传sku
//                array_push($result,$result_spec);
//                array_push($result,$skus);
//                array_push($result,$result_goods);
//                array_push($result,$result_skus);
//            }
//
//
//
//        }
//
//
//        return res('上传成功',$result);
//
//    }

    //上传商品
//    public function upGoods($data,$spec_id,$skus){
//        $goods_data =   '';
//        if(!empty($spec_id)){
//            $brand_id       =   $data['brand_id'] ?? '14322';//美丽演绎
//            $discount_price =   $data['prod_price']*100;    //活动价
//            $out_product_id =   $data['prod_no'];    //产品id
//            $name           =   $data['prod_name'];  //  产品名称
//            $market_price   =   $data['prod_price']*100;
//            $product_format =   $data['product_format'] ?? '14322';//美丽演绎
//            $mobile         =   $data['mobile'] ?? '15359780390';
//            $third_cid      =   $data['third_cid'] ?? '408';
//            /*产品图 xx|xx 详情图  xx|xx*/
//            $img    =   $data['prod_img'];
//            $pic    =   implode("|",$img);
//            $desc   =   implode("|",$img);
//            /*sku图片spec*/
//            $arrIds = [];
//            foreach ($skus as $k=>$v){
//                $ids = explode('|',$v['spec_detail_ids']);
//                array_push($arrIds,$ids[0]);
//            }
//            $arrIds     =   array_unique($arrIds);
//            $spec_pic   =   implode("|^",$arrIds);
//
//            $APP_KEY    =   $data['app_key'] ?? '3356345407633543075';
//            $APP_SECRET =   $data['app_secret'] ?? '1147f03780a21832c72fc608952c2bec';
//            $param_json =   '{"brand_id":"'.$brand_id.'","cos_ratio":"1","description":"'.$desc.'","discount_price":"'.$discount_price.'","first_cid":"5","market_price":"'.$market_price.'","mobile":"'.$mobile.'","name":"'.$name.'","out_product_id":"'.$out_product_id.'","pay_type":"1","pic":"'.$pic.'","product_format":"'.$product_format.'","second_cid":"48","spec_id":"'.$spec_id.'","spec_pic":"'.$spec_pic.'","third_cid":"'.$third_cid.'","weight":"10"}';
//
//
//
//            $attributes['APP_KEY']      =   $APP_KEY;
//            $attributes['APP_SECRET']   =   $APP_SECRET;
//            $attributes['timestamp']    =   date('Y-m-d H:i:s', time());
//            $attributes['c']            =   'product';
//            $attributes['a']            =   'add';
//            $attributes['param_json']   =   $param_json;
//
//
//            $goods_data =   $this->fxgService->sendFxgRequest($attributes);
//        }
//
//
//        return $goods_data;
//    }

    //上传sku信息
//    public function upGoodsSkus($spec_id,$skus,$data){
//
//        $product_id         =   $data['goods_id'];
//        $price              =   '';
//        $detail_id          =   '';
//        $spec_detail_ids    =   '';
//        $stock_num          =   '';
//        $index              =   0;
//        $specId             =   '';
//        $settlement_price   =   '';
//        $code               =   '';
//        foreach ($skus  as   $k=>$v){
//            $detail_id      .=      $k;
//            $price          .=      $v['price'];
//            $spec_detail_ids.=      $v['spec_detail_ids'];
//            $stock_num      .=      $v['stock'];
//            $specId         .=      $spec_id;
//            $settlement_price   .=  '0';
//            $code           .=      '';
//            if($index+1 < count($skus)){
//                $detail_id      .=      '|';
//                $price          .=      '|';
//                $spec_detail_ids.=      '^';
//
//                $stock_num      .=      '|';
//                $specId         .=      '|';
//                $settlement_price   .=  '|';
//                $code           .=      '|';
//            }
//            $index++;
//        }
//
//            $param_json =   '{"code":"'.$code.'","out_product_id":"'.$product_id.'","out_sku_id":"'.$detail_id.'","price":"'.$price.'","settlement_price":"'.$settlement_price.'","spec_detail_ids":"'.$spec_detail_ids.'","spec_id":"'.$specId.'","stock_num":"'.$stock_num.'"}';
//
//
//
//            $APP_KEY    =   $data['app_key'] ?? '3356345407633543075';
//            $APP_SECRET =   $data['app_secret'] ?? '1147f03780a21832c72fc608952c2bec';
//
//            $attributes['APP_KEY']      =   $APP_KEY;
//            $attributes['APP_SECRET']   =   $APP_SECRET;
//            $attributes['timestamp']    =   date('Y-m-d H:i:s', time());
//            $attributes['c']            =   'sku';
//            $attributes['a']            =   'addAll';
//            $attributes['param_json']   =   $param_json;
//
//
//            $sku_data =   $this->fxgService->sendFxgRequest($attributes);
//
//        return $sku_data;
//    }


    //sku上传数据处理
//    public function makeSkus($result_spec,$data){
//        $arr_spec           =   [];
//        $spec               =   $result_spec['data']['Specs'] ?? [];
//        $spec1              =   [];
//        $spec2              =   [];
//        foreach ($spec as $k=>$v){    //三维转二维
//            foreach ($v['values'] as $kk=>$vv ){
//                array_push($arr_spec,$vv);
//            }
//            if($k == 0){
//                $spec1  =   $v['values'];
//            }
//            if($k == 1){
//                $spec2  =   $v['values'];
//            }
//
//        }
//        $sku            =   $data['skus'];
//        $result_arr     =   [];
//        foreach ($spec1 as $k=>$v){
//            foreach ($spec2 as $kk=>$vv){
//                $color_data     =   collect($sku)->where('sku_color',$v['name'])->where('sku_size',$vv['name'])->first();
//                $str1           =   substr($v['id'],-2);
//                $str2           =   substr($vv['id'],-2);
//                $index          =   $str1.$str2;//生成out_sku_id
//
//                if(!empty($color_data)){
//
//                    $result_arr[$index]['spec_detail_ids'] =   $v['id'].'|'.$vv['id'];
//                    $result_arr[$index]['name']    =   $color_data['sku_color'].'-'.$color_data['sku_size'] ?? '';
//                    $result_arr[$index]['price']   =   $data['prod_price']*100 ?? '100000';
//                    $result_arr[$index]['stock']   =   $color_data['sku_stock'] ?? '999';
//                }else{
//
//                    $result_arr[$index]['spec_detail_ids'] =   $v['id'].'|'.$vv['id'];
//                    $result_arr[$index]['name']    =   $v['name'].'-'.$vv['name'] ?? '';
//                    $result_arr[$index]['price']   =   '1000000';
//                    $result_arr[$index]['stock']   =   $color_data['sku_stock'] ?? '0';
//                }
//
//            }
//        }
//
//        return $result_arr;
//    }

    //给已有产品增加sku
//    public function editGoodsSkus($data){
//
//        $product_id         =   $data['goods_id'];
//        $price              =   '';
//        $detail_id          =   '';
//        $spec_detail_ids    =   '';
//        $stock_num          =   '';
//        $index              =   0;
//        $specId             =   '';
//        $settlement_price   =   '';
//        $code               =   '';
//        $skus   =   $data['sku'];
//        $spec_id=   $data['spec_id'];
//        foreach ($skus  as   $k=>$v){
//            $detail_id      .=      $k;
//            $price          .=      $v['price'];
//            $spec_detail_ids.=      $v['spec_detail_ids'];
//            $stock_num      .=      $v['stock'];
//            $specId         .=      $spec_id;
//            $settlement_price   .=  '0';
//            $code           .=      '';
//            if($index+1 < count($skus)){
//                $detail_id      .=      '|';
//                $price          .=      '|';
//                $spec_detail_ids.=      '^';
//
//                $stock_num      .=      '|';
//                $specId         .=      '|';
//                $settlement_price   .=  '|';
//                $code           .=      '|';
//            }
//            $index++;
//        }
//
//        $param_json =   '{"code":"'.$code.'","out_sku_id":"'.$detail_id.'","price":"'.$price.'","product_id":"'.$product_id.'","settlement_price":"'.$settlement_price.'","spec_detail_ids":"'.$spec_detail_ids.'","spec_id":"'.$specId.'","stock_num":"'.$stock_num.'"}';
//
//
//
//        $APP_KEY    =   $data['app_key'] ?? '3356345407633543075';
//        $APP_SECRET =   $data['app_secret'] ?? '1147f03780a21832c72fc608952c2bec';
//
//        $attributes['APP_KEY']      =   $APP_KEY;
//        $attributes['APP_SECRET']   =   $APP_SECRET;
//        $attributes['timestamp']    =   date('Y-m-d H:i:s', time());
//        $attributes['c']            =   'sku';
//        $attributes['a']            =   'addAll';
//        $attributes['param_json']   =   $param_json;
//
//
//        $sku_data =   $this->fxgService->sendFxgRequest($attributes);
//
//        return $sku_data;
//    }
//
//
//    //整理sku
//    public function makeSkus($data){
//        $arr_spec           =   [];
//        $spec               =   $data['specs'];
//        $spec1              =   [];
//        $spec2              =   [];
//        foreach ($spec as $k=>$v){    //三维转二维
//            foreach ($v['values'] as $kk=>$vv ){
//                array_push($arr_spec,$vv);
//            }
//            if($k == 0){
//                $spec1  =   $v['values'];
//            }
//            if($k == 1){
//                $spec2  =   $v['values'];
//            }
//
//        }
//        $sku            =   $data['skus'];
//        $result_arr     =   [];
//        foreach ($spec1 as $k=>$v){
//            foreach ($spec2 as $kk=>$vv){
//                $color_data     =   collect($sku)->where('sku_color',$v['name'])->where('sku_size',$vv['name'])->first();
//                $str1           =   substr($v['id'],-2);
//                $str2           =   substr($vv['id'],-2);
//                $index          =   $str1.$str2;//生成out_sku_id
//
//                if(!empty($color_data)){
//
//                    $result_arr[$index]['spec_detail_ids'] =   $v['id'].'|'.$vv['id'];
//                    $result_arr[$index]['name']    =   $color_data['sku_color'].'-'.$color_data['sku_size'] ?? '';
//                    $result_arr[$index]['price']   =   $color_data['price']*100 ?? '100000';
//                    $result_arr[$index]['stock']   =   $color_data['sku_stock'] ?? '999';
//                }else{
//
//                    $result_arr[$index]['spec_detail_ids'] =   $v['id'].'|'.$vv['id'];
//                    $result_arr[$index]['name']    =   $v['name'].'-'.$vv['name'] ?? '';
//                    $result_arr[$index]['price']   =   '1000000';
//                    $result_arr[$index]['stock']   =   $color_data['sku_stock'] ?? '0';
//                }
//
//            }
//        }
//
//        return $result_arr;
//    }

    //放心购插件上传规格
    public function createSku(Request $request){
        $prod_no   =   $request->input('prod_no') ?? '';
        $shop_id   =   $request->input('shop_id') ?? '';
        $skus      =   $request->input('skus') ?? '';
        $goods_id  =   $request->input('goods_id') ?? '';
        $result =   [];
//        if(!empty($prod_no) && !empty($shop_id)){
//            $shops          =   file_get_contents('http://order.youdnr.com/api/temai/order/fxg_shop_list');
//            $shops          =   json_decode($shops,true);
//            $shops          =   collect($shops['result'])->where('shop_id',$shop_id)->first();
//            $result['shop'] =   $shops;
//            $APP_KEY        =   $shops['appKey'] ?? '';
//            $APP_SECRET     =   $shops['appSecret'] ?? '';
//
//            if(!empty($APP_KEY) && !empty($APP_SECRET)){
//                $data['app_key']        =   $APP_KEY;
//                $data['app_secret']     =   $APP_SECRET;
//                $data['skus']           =   $skus;
//                $data['prod_name']      =   $prod_no.'-'.strtotime(date("Y-m-d H:i:s"));
//                $data['goods_id']       =   $goods_id;
//                $spec =   $this->upGoodsSpec($data);
//                $result['spec_id']        =   $spec['data']['id'] ?? ''; //规格id
//                //$this->editGoods($data);//修改产品信息
//                //$data['specs']  =   $spec['data']['Specs']  ?? [];
//                //$skus_data      =   $this->makeSkus($data);//整理sku
//                //$data['sku']           =   $skus_data;
//                //$up_sku =   $this->editGoodsSkus($data);//上传sku
//            }
//
//        }

        $product = Product::where('prod_no',$prod_no)->first();
        $manu_name  = $product->manu_name ?? '';
        $manu_no    = $product->manu_no ?? '';

        $shops          =   file_get_contents('http://order.youdnr.com/api/temai/order/fxg_shop_list');
        $shops          =   json_decode($shops,true);
        $shops          =   collect($shops['result'])->where('shop_id',$shop_id)->first();
        $result['shop'] =   $shops;
        $APP_KEY        =   $shops['appKey'] ?? '';
        $APP_SECRET     =   $shops['appSecret'] ?? '';

        $data['app_key']        =   $APP_KEY;
        $data['app_secret']     =   $APP_SECRET;
        $data['skus']           =   $skus;
        $data['prod_name']      =   $manu_no.'+'.$manu_name.'-'.$prod_no;
        $data['goods_id']       =   $goods_id;
        $spec =   $this->fxgService->addGoodsSpec($data);
        $spec_id        =   $spec['data']['id'] ?? ''; //规格id

        return $spec_id;

    }
}